<template>
  <div id="container">
    <mt-header title="聊天室"></mt-header>
    <div class="crheader">
      <img :src="face" alt />
      <span v-text="uname">姓名</span>
    </div>
    <div class="chatarea">
      <div class="chart-item" v-for="(k,i) of data" :key="i">
        <div class="user-face">
          <img src="../assets/images/avatar/41.jpg" alt />
        </div>

        <div class="user-message" v-text="k.text"></div>
      </div>
      <div class="user-logined" id="user-logined" v-show="logined">
        <span id="logined-user" v-text="backuname"></span>上线了
      </div>
    </div>
    <div class="sendtext">
      <textarea name id="message" cols="32" rows="4" v-model="text"></textarea>
      <mt-button type="primary" size="small" id="btn" @click="send()">发送</mt-button>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      text: "",
      uname: this.$route.query.uname,
      face: "../assets/images/avatar/" + this.$route.query.face,
      logined: false,
      backuname: "",
      n: 0,
      data: []
    };
  },
  methods: {
    send() {
      this.$sockets.emit("onmessage", {
        text: this.text,
        face: this.face,
        uname: this.backuname
      });
    }
  },
  mounted() {
    this.$sockets.emit("logined", { message: this.uname });
    //console.log("发送请求");

    this.$sockets.on("welcome", data => {
      //console.log(data);
      this.backuname = data.message;
      this.logined = true;
      setTimeout(() => {
        this.logined = false;
      }, 3000);
    });
    this.$sockets.on("backme", data => {
      console.log(data);
      console.log(this.n);
      this.data[this.n] = data;
      this.n++;
      console.log(this.data);
      
      /* var chatareaEle = document.getElementsByClassName("chatarea")[0];
      var messageItem = document.createElement("div");
      messageItem.innerHTML = `<div class="chart-item">
                     <div class="user-face">
                    <img src=${data.face} alt="">
                    </div> 
                    <div>${data.uname}</div>
                    <div class="user-message">${data.text}</div>
                    </div>`;
      chatareaEle.appendChild(messageItem); */
    });
  }
};
</script>
<style scoped>
#container {
  display: flex;
  flex-direction: column;
  width: 100%;
  margin-bottom: 50px;
}
.crheader {
  height: 15%;
  background-color: cornflowerblue;
}
.crheader img {
  float: left;
  margin-left: 20px;
}
.crheader span {
  display: inline-block;
  font-size: 20px;
  line-height: 80px;
  vertical-align: middle;
}
.chatarea {
  height: 50%;
  border: solid 1px black;
  height: 400px;
  background-color: beige;
  position: relative;
}
.sendtext {
  position: relative;
  width: 100%;
  height: 66px;
}
#btn {
  position: absolute;
  bottom: 0px;
  right: 0;
  z-index: 0;
}
#message {
  font-size: 16px;
  width: 99%;
  height: 60px;
  resize: none;
}
.chart-item {
  float: left;
  height: 60px;
  line-height: 60px;
  vertical-align: middle;
  width: 70%;
}
.user-face {
  float: left;
  display: flex;
  height: 60px;
  line-height: 60px;
  align-items: center;
  justify-content: center;
}
.user-face img {
  width: 50px;
  height: 50px;
  border-radius: 5px;
}
.user-message {
  float: left;
  min-width: 40%;
  max-width: 70%;

  background-color: white;
  border: solid 1px black;
  border-radius: 5px;
  margin-left: 5px;
  margin-top: 5px;
  white-space: normal;
  padding-left: 5px;
}
.user-logined {
  width: 50%;
  height: 40px;
  line-height: 40px;
  border: solid 1px black;
  border-radius: 5px;
  background-color: cyan;
  position: absolute;
  bottom: 0px;
  left: 50%;
  margin-left: -100px;
}
</style>